import { loginByPasswordApi } from '@/api/system/user'

import type { LoginByPassword } from '@/types/user'
import { defineStore } from 'pinia'
import { reactive, ref } from 'vue'

export const useLoginStore = defineStore(
  'bc-login',
  () => {
    const isRemPwd = ref<boolean>()
    const account = reactive<LoginByPassword>({ username: '', password: '' })
    const token = ref<String>()

    // 密码登录
    const loginByPasswordAction = async () => {
      const res = await loginByPasswordApi(account)
      console.log(res);
      
      token.value = res.data.accessToken
      // 是否记住密码
      if (!isRemPwd.value) {
        account.username = ''
        account.password = ''
      }
    }
    const delToken = () => {
      token.value = ''
    }
    return {
      isRemPwd,
      token,
      account,
      loginByPasswordAction,
      delToken
    }
  },
  {
    persist: true
  }
)
